<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="ru" xml:lang="ru"> 
    <head>
	<title>LocalCRM</title>
	<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
	<script src="js/jquery-ui-1.8.4.custom.min.js" type="text/javascript"></script>
	<script src="js/jquery.blockUI.js" type="text/javascript"></script>
	<script src="js/jquery.contextMenu.js" type="text/javascript"></script>

	<script src="js/i18n/grid.locale-ru.js" type="text/javascript"></script>
	<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>

	<script src="js/crm/common.js" type="text/javascript"></script>
	<link href="css/redmond/jquery-ui-1.8.4.custom.css" rel="stylesheet" type="text/css"/>
	<link href="css/jquery.contextMenu.css" rel="stylesheet" type="text/css"/>
	<link href="css/ui.jqgrid.css" rel="stylesheet" type="text/css"/>

{if empty($smarty.get.mode)}
	<style type="text/css">
	<!--{literal}
	html,body	{ padding:0; margin: -3px; }
	{/literal}-->
	</style>
{/if}
    </head>
    <body>
{if not empty($smarty.get.mode)}
	<script type="text/javascript">
	<!--{literal}
	$(document).ready
	(
	    function()
	    {
		$(window).bind
		(
		    'resize',
		    function()
		    {
			$('.grid')
			    .setGridWidth($(document).width()-20)
			    .setGridHeight($(document).height()-100)
			    .trigger('resize');
		    }
		);
	    }
	);
	{/literal}-->
	</script>
{/if}

{include file="admin/submodules/_menu.html"}

{if $smarty.get.mode == 'unallocated-all'}
	<script type="text/javascript">
	<!--{literal}
	$(document).ready
	(
	    function()
	    {
		$('table.grid#calls-all').jqGrid
		({
		    url:	'calls.php?mode=unallocated-all',
		    datatype:	'json',
		    colNames:	['ID','Номер','Куда','Время','Продолжительность','Направление'],
		    colModel:
		    [
			{name:'id',hidden:true},
			{name:'src'},
			{name:'dst'},
			{name:'date'},
			{name:'duration'},
			{name:'title'}
		    ],
		    sortname:	'date',
		    sortorder:	'desc',
		    rowNum:	100,
		    pager:	'calls-all-pager',
		    multiselect:	true
		});

		$(window).trigger('resize');

		$('input:button#calls-all-delete').click
		(
		    function()
		    {
			var rows = $('table.grid#calls-all').getGridParam('selarrrow');

			if ( rows.length>0 )
			{
			    if ( window.confirm('Вы дейстительно хотите удалить записи ('+rows.length+') из БД?') )
			    {
				$.getJSON
				(
				    'calls.php',
				    {
					do:	'delete',
					id:	rows
				    },
				    function(json){if(json['code']==200){$('.grid').trigger('reloadGrid');}}
				);
			    }
			}
			else
			{
			    alert('Ничего не отмечено!');
			}
		    }
		);
	    }
	);
	{/literal}-->
	</script>

	<table class="grid" id="calls-all"></table>
	<div id="calls-all-pager"></div>

	<input type="button" class="button" id="calls-all-delete" value="Удалить выбранные"/>
{elseif $smarty.get.mode == 'unallocated-directions'}
	<script type="text/javascript">
	<!--{literal}
	$(document).ready
	(
	    function()
	    {
		var Url;

		if ( $('input:hidden#phone-direction-id').val()>0 )
		{
		    Url = 'calls.php?mode=unallocated-directions&phone_direction_id='+$('input:hidden#phone-direction-id').val();
		}
		else
		{
		    Url = 'calls.php?mode=unallocated-directions';
		}

		$('table.grid#unallocated-directions').jqGrid
		({
		    url:	Url,
		    datatype:	'json',
		    colNames:	['ID','Дата','Номер'],
		    colModel:
		    [
			{name:'id',hidden:true},
			{name:'date',width:40},
			{name:'dst',width:300}
		    ],
		    sortname:	'date',
		    sortorder:	'desc',
		    rowNum:	100,
		    pager:	'unallocated-directions-pager',
		    ondblClickRow:	function(rowid,iRow,iCol,e)
					{
					    $('div.dialog#direction-add')
						.dialog
						(
						    'option',
						    {
							title:	'Добавление направления',
							resizable:	false,
							buttons:
							{
							    'Добавить':	function()
									{
									    $.getJSON
									    (
										'calls.php',
										{
										    do:		'add_direction',
										    prefix:	$('input:text#prefix').val(),
										    name:	$('input:text#name').val(),

										    id:		rowid,
										    phone_direction_id:	$('input:hidden#phone-direction-id').val()
										},
										function(json){if(json['code']==200){$('div.dialog#direction-add').dialog('close');}}
									    );
									}
							}
						    }
						)
						.find('input:text#prefix')
						    .val( $(this).getCell(rowid,iCol) )
						    .end()
						.dialog('open');
					}
		});

		$(window).trigger('resize');
	    }
	);
	{/literal}-->
	</script>

{if $smarty.get.mobile == 'yes'}
	<input type="hidden" id="phone-direction-id" value="11401"/>
{/if}

	<div class="dialog" id="direction-add">
	    <table width="100%">
		<tr>
		    <td align="right"><strong>Префикс:</strong></td>
		    <td><input type="text" id="prefix"/></td>
		</tr>
		<tr>
		    <td align="right"><strong>Имя:</strong></td>
		    <td><input type="text" id="name"/></td>
		</tr>
	    </table>
	</div>

	<table class="grid" id="unallocated-directions"></table>
	<div id="unallocated-directions-pager"></div>

	<i>Для добавления нового направления, дважды кликните по записи</i>
{elseif $smarty.get.mode == 'unallocated-subscribers'}
	<script type="text/javascript">
	<!--{literal}
	$(document).ready
	(
	    function()
	    {
		$('table.grid#unallocated-subscribers').jqGrid
		({
		    url:	'calls.php?mode=unallocated-subscribers',
		    datatype:	'json',
		    colNames:	['Номер'],
		    colModel:
		    [
			{name:'src',width:300}
		    ],
		    sortname:	'src',
		    rowNum:	100,
		    pager:	'unallocated-subscribers-pager'
		});

		$(window).trigger('resize');
	    }
	);
	{/literal}-->
	</script>

	<strong>Вам необходимо добавить этих абонентов в БД</strong>

	<table class="grid" id="unallocated-subscribers"></table>
	<div id="unallocated-subscribers-pager"></div>
{elseif $smarty.get.mode == 'unallocated-directions-rate'}
	<script type="text/javascript">
	<!--{literal}
	$(document).ready
	(
	    function()
	    {
		$('table.grid#unallocated-directions-rate').jqGrid
		({
		    url:	'calls.php?mode=unallocated-directions-rate',
		    datatype:	'json',
		    colNames:	['ID','Номер','Префикс','Направление'],
		    colModel:
		    [
			{name:'id',hidden:true},
			{name:'dst',width:30},
			{name:'prefix',width:20},
			{name:'title'}
		    ],
		    sortname:	'title',
		    rowNum:	100,
		    pager:	'unallocated-directions-rate-pager',
		    ondblClickRow:	function(rowid,iRow,iCol,e)
					{
					    $('div.dialog#phone-rates')
						.dialog
						(
						    'option',
						    {
							title:	'Добавление направления в тариф',
							width:	800,
							buttons:
							{
							    'Сохранить':	function()
										{
										    var data = new Object;

										    data['do'] = 'save-directions';
										    data['phone_direction_id'] = rowid;

										    $('input:text.rate-zone').each
										    (
											function()
											{
											    if ( $(this).val() )
											    {
												data[$(this).attr('name')] = $(this).val();
											    }
											}
										    );

										    $.getJSON
										    (
											'calls.php',
											data,
											function(json)
											{
											    if(json['code']==200)
											    {
												$('div.dialog#phone-rates').dialog('close');
												$('table.grid#unallocated-directions-rate').trigger('reloadGrid');
											    }
											}
										    );
										}
							}
						    }
						)
						.find('span#direction-name')
						    .html( $(this).getCell(rowid,3) )
						    .end()
						.find('span#direction-prefix')
						    .html( $(this).getCell(rowid,2) )
						    .end()
						.dialog('open');
					}
		});

		$('input:text.rate-zone').keyup
		(
		    function()
		    {
			var input = $(this);

			$.getJSON
			(
			    'calls.php',
			    {
				do:	'get-cost',
				rate_id:	$(this).attr('name'),
				zone_num:	$(this).val()
			    },
			    function(json)
			    {
				if(json['code']==200)
				{
				    $(input)
					.parent()
					.next()
					    .attr('align','right')
					    .html(json['zone']['cost']);
				}
				else
				{
				    $(input)
					.parent()
					.next()
					    .attr('align','center')
					    .html('&mdash;');
				}
			    }
			);
		    }
		);

		$(window).trigger('resize');
	    }
	);
	{/literal}-->
	</script>

	<div class="dialog" id="phone-rates">
	    <input type="hidden" id="rate-id"/>

	    <center>
		Направление: <span id="direction-name" style="font-weight: bold;"></span><br/>
		Префикс: <span id="direction-prefix" style="font-weight: bold;"></span>
	    </center>
	    <br/>
	    <table width="100%" style="border: solid 1px #000;" rules="all">
		<col style="width: 20px;"/>
		<col/>
		<col style="width: 40px;"/>
		<col style="width: 20px;"/>

		<thead>
		    <tr>
			<th>№</th>
			<th>Тариф</th>
			<th>Зона</th>
			<th>Цена</th>
		    </tr>
		</thead>

		<tbody>
		{foreach from=$phone_rates name=rates item=rate}
		    <tr class="rate-id{$rate.id}">
			<td>{$smarty.foreach.rates.iteration}</td>
			<td>{$rate.rate_name}</td>
			<td><input type="text" class="rate-zone" name="{$rate.id}"/></td>
			<td align="center" class="cost">&mdash;</td>
		    </tr>
		{/foreach}
		</tbody>
	    </table>
	</div>

	<table class="grid" id="unallocated-directions-rate"></table>
	<div id="unallocated-directions-rate-pager"></div>

	<i>Для добавления нового направления в тариф, дважды кликните по записи</i>
{else}
	<ul>
	    <li><a href="?mode=unallocated-all">Все записи</a></li>
	    <li><a href="?mode=unallocated-subscribers">Неизвестные абоненты</a></li>
	    <li><a href="?mode=unallocated-directions">Неизвестные направления</a></li>
	    <li><a href="?mode=unallocated-directions-rate">Направления, не входящие в тарифы</a></li>
	    <!--<li><a href="?mode=unallocated-directions&mobile=yes">Мобильные</a></li> -->
	</ul>
{/if}
    </body>
</html>
